<template>
    <div>
        <el-dialog
            :title="info.isAdd ? '角色添加' : '角色编辑'"
            :visible.sync="info.isshow"
            @closed="closed"
        >
            <div>form:{{ form }}</div>
            <el-form :model="form">
                <el-form-item label="角色名称" :label-width="formLabelWidth">
                    <el-input v-model="form.rolename" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色权限" :label-width="formLabelWidth">
                    <el-tree
                        ref="tree"
                        :data="menuList"
                        show-checkbox
                        node-key="id"
                        :props="{ children: 'children', label: 'title' }"
                    ></el-tree>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" v-if="info.isAdd" @click="add">添加</el-button>
                <el-button type="primary" v-else @click="edit">修改</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqroleadd, reqroleinfo, reqmenulist, reqroleedit } from "../../../http/api"
import { IndexRoutes } from "../../../router"
import { errorAlert, successAlert } from "../../../utils/alert"
export default {
    props: ["info", "list"],
    data() {
        return {
            menuList: [],
            form: {
                rolename: "",
                menus: "",
                status: 1,
            },
            formLabelWidth: "120px",
            IndexRoutes,
        }
    },
    computed: {
        ...mapGetters({
            user: "user"
        }),
    },
    mounted() {
        reqmenulist({ istree: true }).then(res => {
            if (res.data.code == 200) {
                this.menuList = res.data.list ? res.data.list : []
            }
        })
    },
    methods: {
        ...mapActions({
            changeUser: "changeUser"
        }),
        //清空表单的数据
        empty() {
            this.form = {
                rolename: "",
                menus: "",
                status: 1,
            }
            // 清空树上的选中的节点
            this.$refs.tree.setCheckedKeys([])
        },

        //输入验证
        checkDate() {
            return new Promise((resolve, rejeck) => {
                //验证
                if (this.form.rolename === "") {
                    errorAlert("角色名称不能为空")
                    return;
                }
                // if (this.form.menus === "") {
                //     errorAlert("角色权限不能一个都不选")
                //     return
                // }

                resolve();
            })
        },

        //点了添加
        add() {
            //验证先
            this.checkDate().then(() => {
                //将书上的数据赋值给form
                this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys())

                reqroleadd(this.form).then(res => {
                    if (res.data.code == 200) {
                        //弹窗成功
                        successAlert(res.data.msg);
                        //调用empty清空表单内容
                        this.empty();
                        //关闭弹框
                        this.info.isshow = false;
                        //刷新列表
                        this.$emit("init")
                    }
                })
            })
        },
        //点了取消
        cancel() {
            this.info.isshow = false
        },
        //修改了上级菜单
        changePid() {
            if (this.form.pid == 0) {
                this.form.type = 1
            } else {
                this.form.type = 2
            }
        },
        //编辑渲染页面
        getOne(id) {
            reqroleinfo({ id: id }).then(res => {
                if (res.data.code == 200) {
                    this.form = res.data.list
                    //补id
                    this.form.id = id
                    //给树赋值
                    this.$refs.tree.setCheckedKeys(JSON.parse(res.data.list.menus))
                }
            })
        },
        //修改
        edit() {
            //先将树上的数据给form.menus 
            this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys())
            //法修改的请求
            reqroleedit(this.form).then(res => {
                if (this.form.id == this.user.roleid) {
                    //修改成功弹窗
                    successAlert("修改成功")
                    //清空表单内容
                    this.form = this.empty();
                    //清空仓库数据
                    this.changeUser({})
                    //跳转登陆
                    this.$router.push("/login");
                    return
                }
                if (res.data.code == 200) {
                    //成功弹窗
                    successAlert(res.data.msg);
                    //清除数据表单
                    this.empty();
                    //关闭窗口
                    this.info.isshow = false
                    //刷新页面
                    this.$emit("init")
                }
            })
        },
        //弹框关闭
        closed() {
            if (this.info.isAdd == false) {
                this.empty()
            }
        }
    },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>